package example.com.zcq.mymj.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import example.com.zcq.mymj.R;
import example.com.zcq.mymj.ui.TopBar;
import example.com.zcq.mymj.util.PreferenceUtils;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TopBar mTopBar;
    /**
     * 输入您申请时的手机号
     */
    private EditText mEdtIdCardNum;
    /**
     * 输入密码
     */
    private EditText mEdtPassword;
    /**
     * 没有账号？
     */
    private TextView mTvNoCard;
    /**
     * 忘记密码？
     */
    private TextView mTvForgetPassword;
    /**
     * 登录
     */
    private Button mBtnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        mTopBar = (TopBar) findViewById(R.id.top_bar);
        mEdtIdCardNum = (EditText) findViewById(R.id.edt_id_card_num);
        mEdtPassword = (EditText) findViewById(R.id.edt_password);
        mTvNoCard = (TextView) findViewById(R.id.tv_no_card);
        mTvForgetPassword = (TextView) findViewById(R.id.tv_forget_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);
        mTvForgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, ForgetPwActivity.class));
            }
        });
        mTvNoCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_login:
                if ("123456".equals(mEdtIdCardNum.getText().toString().trim()) && "123456".equals(mEdtPassword.getText().toString().trim())) {
                    startActivity(new Intent(LoginActivity.this, MainActivity.class).putExtra("name","小新"));
                    PreferenceUtils.write(LoginActivity.this,"namefile","name","小新");
                    Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                    LoginActivity.this.finish();

                } else {
                    Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();

                }

                break;
            case R.id.tv_forget_password:
                startActivity(new Intent(LoginActivity.this, ForgetPwActivity.class));
                break;
            case R.id.tv_no_card:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
        }
    }
}
